<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";
export default {
  props: {
    probeType: {
      type: Number,
      defult: 0,
    },
    pullUpLoad: {
      type: Boolean,
      defult: false,
    },
  },
  data() {
    return {
      scroll: null,
    };
  },
  mounted() {
    //创建bscroll对象
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad,
    });

    //监听滚动的位置
    if (this.probeType === 2 || this.probeType === 3) {
      this.scroll.on("scroll", (position) => {
        // 传递值出去 让需要的组件接收  子向父组件传递值
        this.$emit("scroll", position);
      });
    }

    //监听上拉加载更多
    if (this.pullUpLoad) {
      this.scroll.on("pullingUp", () => {
        // 传递值出去 让需要的组件接收  子向父组件传递值
        this.$emit("pullingUp");
      });
    }
  },
  methods: {
    //返回顶部
    scrollTo(x, y, time = 300) {
      //先判断是否有值 逻辑与 执行
      this.scroll && this.scroll.scrollTo(x, y, time);
    },
    // 上拉加载更多
    finishPullUp() {
      this.scroll && this.scroll.finishPullUp();
    },
    //再次重新计算可滚动高度
    refresh() {
      this.scroll && this.scroll.refresh();
    },
    //设定y值 原状态 判断是后有值
    getScrollY() {
      return this.scroll ? this.scroll.y : 0;
    },
  },
};
</script>

<style scoped>
</style>